package com.ailk.configTask.action;

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.tapestry.IRequestCycle;

import com.ai.appframe2.common.SessionManager;
import com.ai.appframe2.service.ServiceFactory;
import com.ailk.appengine.util.DataBusHelper;
import com.ailk.common.data.IDataBus;
import com.ailk.common.data.impl.DataBus;
import com.ailk.common.data.impl.DataContext;
import com.ailk.configTask.bo.BOUpConfigFlowDetailBean;
import com.ailk.configTask.bo.BOUpConfigFlowNodeBean;
import com.ailk.configTask.bo.BOUpConfigTaskBean;
import com.ailk.configTask.ivalues.IConfigFlowValue;
import com.ailk.configTask.service.interfaces.IUpConfigTaskSV;
import com.ailk.configTask.util.ConfigTaskConsts;
import com.ailk.upc.action.PopupPage;
import com.ailk.upc.action.TaskBasePage;
import com.ailk.upc.util.MatrixUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public abstract class ConfigTaskQueryManm extends TaskBasePage{

	public abstract void setListFormCount(long listFormCount);

	public abstract void setListFormItem(JSONObject listFormItem);

	public abstract void setItemForm(JSONObject itemForm);

	public abstract void setRowIndex(int rowIndex);

	public abstract void setListFormItems(JSONArray listFormItems);

	public abstract void setCondForm(JSONObject condForm);
	
	public abstract void setNewTaskId(long newTaskId);
	public abstract void setCurrentOpId(long curOpId);
	public void addWorkOrderInfo(IRequestCycle cycle) throws Exception {
		IDataBus condBus = createData("configTaskInfo");
		IUpConfigTaskSV sv = (IUpConfigTaskSV) ServiceFactory.getService(IUpConfigTaskSV.class);
		BOUpConfigTaskBean taskValue = (BOUpConfigTaskBean)DataBusHelper.getBOBean(condBus);
		
		getContext().setAjax(
				sv.createConfigTask(taskValue));		
	}
	
	protected void initPageAttrs() {
		bindBoName("configTaskInfo", "com.ailk.configTask.bo.BOUpConfigTask");
	}

	public void init(IRequestCycle cycle) throws Exception {
		// 设置任务信息
		IUpConfigTaskSV taskSv = (IUpConfigTaskSV) ServiceFactory.getService(IUpConfigTaskSV.class);
		long taskId = taskSv.getNewTaskId();	
		setNewTaskId(taskId);
		getContext().getSession().setAttribute("CurrentTaskId",
				new Long(taskId));
		setCurrentOpId(SessionManager.getUser().getID());
		/*自测1*/
		setDzText("邓慧智");
		/*自测2*/
		JSONArray list = new JSONArray();
		JSONObject a = new JSONObject();
		a.put("id", 1);
		a.put("name", "denghuizhi");
		JSONObject b = new JSONObject();
		b.put("id", 2);
		b.put("name", "denghuicong");
		list.add(a);
		list.add(b);
		setCustomerList(list);
		setCustomer(a);
	}
	
	public IDataBus getFlow(IRequestCycle cycle) throws Exception {
		// 设置任务信息
		long flowId = getContext().getData().getLong("flowId");
		IUpConfigTaskSV taskSv = (IUpConfigTaskSV) ServiceFactory.getService(IUpConfigTaskSV.class);
		IConfigFlowValue flow = taskSv.getFlowByFlowId(flowId);
		JSONObject flowInfo = new JSONObject();
		JSONArray arr = new JSONArray();
		arr.add(flowInfo);
		IDataBus rtn = new DataBus(new DataContext(), arr);
		flowInfo.put("groupId", 0);
		if(flow != null)
		{
			BOUpConfigFlowNodeBean startFlow = flow.getStartFlowNode();
			if(startFlow == null) return rtn;
				
			Map<Long, BOUpConfigFlowDetailBean> detailBeans = flow.getNextFlowDetails(startFlow.getNodeId(), ConfigTaskConsts.RESULT_OK);
			if( detailBeans == null) return rtn;
			for (Entry<Long, BOUpConfigFlowDetailBean> ite  : detailBeans.entrySet()) {
				BOUpConfigFlowDetailBean detailBean = ite.getValue();
				long nextNodeId = detailBean.getNextNodeId();
				BOUpConfigFlowNodeBean nextNode = flow.getFlowNode(nextNodeId);
				if(nextNode != null){
					flowInfo.put("groupId", nextNode.getEntityId());
					break;
				}
			}

		}
		return rtn;
	}
	
	public void generateImg(IRequestCycle cycle) throws Exception {
		// 设置任务信息
		String content = getContext().getData().getString("ImgContent");
		JSONObject imgInfo = new JSONObject();
		JSONArray arr = new JSONArray();
		arr.add(imgInfo);
		String writeFileName1 = "img/barcode/barcode.gif" ;
		String  writeFileName = getContext().getSession().getServletContext().getRealPath("/"+writeFileName1);
		IDataBus rtn = new DataBus(new DataContext(), arr);
		String fileName = cycle.getRequestContext().getAbsoluteURL(cycle.getRequestContext().getRequestURI() + "img/barcode/barcode.gif");
		//String fileName = cycle.getRequestContext().getAbsoluteURL(writeFileName);
		File outputFile = new File(writeFileName);
		String format = "gif";
//		
        MatrixUtil.writeToFile(MatrixUtil.toBarCodeMatrix(content, null, null), format, outputFile);	
		imgInfo.put("fileName", "img/barcode/barcode.gif");
		imgInfo.put("listener", "init");
		getContext().setAjax(imgInfo);
	}	
	
	
	/*---------------自己的测试1，显示文字-----------------------------*/
	
//	  public abstract String getDzText();
	public abstract void setDzText(String dzText);
	
	/*---------------自己的测试2，显示列表-----------------------------*/
	public abstract void setCustomerList(JSONArray customerList);
	public abstract void setCustomer(JSONObject customer);
	  
    /*---------------自己的测试3，ajax参数获取-----------------------------*/
	public abstract void setTestInfo(JSONObject testInfo);
	public void addTestInfo(IRequestCycle cycle) throws Exception {
		IDataBus condBus = createData("testInfo");
		JSONObject o = condBus.getDataObject();
		String testId = o.getString("testId");
		String testName = o.getString("testName");
		System.out.println("here is start.");
		System.out.println(testId);
		System.out.println(testName);
		setDzText(testName);
		System.out.println("here is success.");
	}
	/*---------------自己的测试4，返回JSON-----------------------------*/
	/*---------------自己的测试5，局部刷新-----------------------------*/
}
